﻿using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Routing;

namespace DiskCaching
{
   public class Global : System.Web.HttpApplication
   {
      private const string pdfToImageUrl = "pageasimage.ashx";
      private static Regex cacheToDisk = new Regex( ".*"+pdfToImageUrl+".*" );

      /// <summary>
      /// Gets the name of the default output-cache provider that is configured for a Web site.
      /// </summary>
      /// <param name="context">An <see cref="T:System.Web.HttpContext"/> that provides references to intrinsic server objects that are used to service HTTP requests.</param>
      /// <returns>The name of the default provider.</returns>
      /// <exception cref="T:System.Configuration.Provider.ProviderException">
      /// 	<paramref name="context"/> is null or is an empty string.</exception>
      public override string GetOutputCacheProviderName( HttpContext context )
      {
         // If the URL matches the name of the PdfToImage handler
         // use disk based caching
         if ( cacheToDisk.IsMatch(context.Request.RawUrl) )
         {
            return "DiskCache";
         }

         // Otherwise, use normal output caching
         return base.GetOutputCacheProviderName( context );
      }

      /// <summary>
      /// Handles the Start event of the Application control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
      void Application_Start( object sender, EventArgs e )
      {

      }
   }
}
